import type { UserProfile } from "@/apis/user"

// 存储工具函数
const TOKEN_KEY = 'interview_token'
const USER_PROFILE_KEY = 'interview_user_profile'

/**
 * 获取token
 */
export const getStoredToken = (): string | null => {
  return localStorage.getItem(TOKEN_KEY)
}

/**
 * 设置token
 */
export const setStoredToken = (token: string): void => {
  localStorage.setItem(TOKEN_KEY, token)
}

/**
 * 删除token
 */
export const removeStoredToken = (): void => {
  localStorage.removeItem(TOKEN_KEY)
}

/**
 * 获取存储的用户信息
 */
export const getStoredUserProfile = (): UserProfile | null => {
  try {
    const userStr = localStorage.getItem(USER_PROFILE_KEY)
    return userStr ? JSON.parse(userStr) : null
  } catch (error) {
    console.error('获取用户信息失败:', error)
    return null
  }
}

/**
 * 存储用户信息
 */
export const setStoredUserProfile = (user: UserProfile): void => {
  try {
    localStorage.setItem(USER_PROFILE_KEY, JSON.stringify(user))
  } catch (error) {
    console.error('保存用户信息失败:', error)
  }
}

/**
 * 删除存储的用户信息
 */
export const removeStoredUserProfile = (): void => {
  localStorage.removeItem(USER_PROFILE_KEY)
}
